package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.BackBillOptionActivity;
import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/18.
 */

public class BackBillAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<OutputBill> mDatas;
    private Context mContext;
    private int type;

    public BackBillAdapter(List<OutputBill> list, Context mContext, int type) {
        this.mDatas = list;
        this.mContext = mContext;
        this.type = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        mContext = parent.getContext();
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.back_bill_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.isEmpty()) {
            return TYPE_EMPTY;
        } else {
            return TYPE_DEFAULT;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            Object obj = mDatas.get(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.bindViewData((OutputBill) obj);
            viewHolder.setViewListener((OutputBill) obj, position);
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas != null && mDatas.size() > 0) {
            return mDatas.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_EMPTY:
                return 0;
            default:
                return 1;
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowImageView;
        ExpandView expandView;
        TextView statusLabelTV;
        TextView receivedCompanyTV;
        TextView outTimeTV;
        TextView grossLabelTV;
        TextView grossWeightTV;
        TextView treLabelTV;
        TextView treWeightTV;
        TextView netLabelTV;
        TextView netWeightTV;
        TextView billNumberTV;
        TextView supplyCompanyTV;
        TextView coalNameTV;
        TextView carInformationTV;
        Button option_btn;
        TextView carNumberTV;
        TextView ObNumberTV;

        public ViewHolder(View itemView) {
            super(itemView);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            arrowImageView = (ImageView) itemView.findViewById(R.id.arrow);
            receivedCompanyTV = (TextView) itemView.findViewById(R.id.received_company);
            outTimeTV = (TextView) itemView.findViewById(R.id.out_time);
            grossLabelTV = (TextView) itemView.findViewById(R.id.gross_label);
            grossWeightTV = (TextView) itemView.findViewById(R.id.gross_weight);
            treLabelTV = (TextView) itemView.findViewById(R.id.tre_label);
            treWeightTV = (TextView) itemView.findViewById(R.id.tre_weight);
            netLabelTV = (TextView) itemView.findViewById(R.id.net_label);
            netWeightTV = (TextView) itemView.findViewById(R.id.net_weight);
            billNumberTV = (TextView) itemView.findViewById(R.id.bill_number);
            supplyCompanyTV = (TextView) itemView.findViewById(R.id.supply_company);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            carInformationTV = (TextView) itemView.findViewById(R.id.car_information);
            option_btn = (Button) itemView.findViewById(R.id.option_btn);
            carNumberTV = (TextView) itemView.findViewById(R.id.car_number);
            ObNumberTV = (TextView) itemView.findViewById(R.id.ob_number);
        }

        public void bindViewData(OutputBill bill) {
            receivedCompanyTV.setText(bill.getBuycompany());
            if (bill.getReceivedtime() != null) {
                outTimeTV.setText(TimeUtils.parseDateTime(bill.getReceivedtime()));
            } else {
                outTimeTV.setText(TimeUtils.parseDateTime(bill.getOuttime()));
            }

            carInformationTV.setText(bill.getCarnumber()
                    + "  " + bill.getCarowner() + "  "
                    + BaseActivity.formatPhoneNumber(bill.getCarconnectphone()));
            billNumberTV.setText(bill.getObnum());
            supplyCompanyTV.setText(bill.getSendcompany());
            coalNameTV.setText(bill.getCoaltype());
            carNumberTV.setText(bill.getCarnumber());
            ObNumberTV.setText(bill.getObnum());
            if (bill.getIsreceived() == 1) {
                option_btn.setVisibility(View.GONE);
                statusLabelTV.setText(mContext.getResources().getString(R.string.served));
                grossLabelTV.setText("原发净重");
                treLabelTV.setText("收货净重");
                netLabelTV.setText("磅差");
                grossWeightTV.setText(bill.getSendnetweight() + "");
                treWeightTV.setText(bill.getInputnetweight() + "");
                netWeightTV.setText(bill.getLossweight() + "");
            } else {
                option_btn.setVisibility(View.VISIBLE);
                statusLabelTV.setText(mContext.getResources().getString(R.string.no_served));
                grossLabelTV.setText("毛重");
                treLabelTV.setText("皮重");
                netLabelTV.setText("净重");
                grossWeightTV.setText(bill.getSendgrossweight() + "");
                treWeightTV.setText(bill.getSendtareweight() + "");
                netWeightTV.setText(bill.getSendnetweight() + "");
            }

        }

        public void setViewListener(final OutputBill bill, final int position) {
            arrowImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (expandView.isExpand()) {
                        expandView.collapse();
                        arrowImageView.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                    } else {
                        expandView.expand();
                        arrowImageView.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                    }
                }
            });
            option_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("backBill", bill);
                    bundle.putInt("postition", position);
                    Intent intent = new Intent(mContext, BackBillOptionActivity.class);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        }
    }

}
